package com.kcst.configure;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridLayout;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.constraintlayout.widget.ConstraintLayout;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author: glc.
 * Date: 2022/8/12  15:49
 * Description:
 */
public class ConfigureSettingView extends GridLayout {

    private Context mContext;
    private Map<String,AppCompatEditText> mFieldsView = new HashMap<>();

    public ConfigureSettingView(Context context) {
        this(context,null);
    }

    public ConfigureSettingView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ConfigureSettingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        int arrangeMaxRows = ConfigureManager.getInstance().getArrangeMaxRows();
        this.setColumnCount(3);
        this.setRowCount(arrangeMaxRows);
        this.setOrientation(GridLayout.VERTICAL);
    }

    public void initView(Map<String,String> configureValues){
        for (String fields : configureValues.keySet()) {
            String name = configureValues.get(fields);
            View itemView = LayoutInflater.from(mContext)
                    .inflate(R.layout.layout_config_item, null);
            ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.WRAP_CONTENT,
                    ConstraintLayout.LayoutParams.WRAP_CONTENT);
            LayoutParams layoutParamss = new LayoutParams(layoutParams);
            layoutParamss.setMargins(120,20,30,0);
            AppCompatTextView tVTitle = itemView.findViewById(R.id.tv_item_car_title);
            AppCompatEditText eTValue = itemView.findViewById(R.id.tv_item_car_value);
            tVTitle.setText(name);
            itemView.setLayoutParams(layoutParamss);
            mFieldsView.put(fields,eTValue);
            addView(itemView);
        }
        setLocalFieldValue();
    }

    private void setLocalFieldValue(){
        for (String fields : mFieldsView.keySet()) {
            AppCompatEditText et = mFieldsView.get(fields);
            String fieldsValue = ConfigureManager.getInstance().getFieldsValue(fields);
            et.setText(fieldsValue);
        }
    }

    public void saveValue(){
        Map<String,String> setValues = new HashMap<>();
        StringBuffer sb = new StringBuffer();
        for (String fields : mFieldsView.keySet()) {
            AppCompatEditText etValue = mFieldsView.get(fields);
            assert etValue != null;
            String value = Objects.requireNonNull(etValue.getText()).toString().trim();
            ConfigureManager.getInstance().setFieldsValue(fields,value);
            setValues.put(fields,value);
            sb.append(fields).append("=").append(value).append(";").append("\r\n");
        }

        SpUtils.putMap("FIELDS_KEY",setValues);
        FileUtils.write(sb.toString());
        Toast.makeText(mContext, "保存成功", Toast.LENGTH_SHORT).show();
    }
}
